<?php

//公共函数

class PublicAction extends Action{
    /**
	 * 验证码
	 */
	public function verify() {
        $type	 =	 isset($_GET['type'])?$_GET['type']:'gif';
        import("ORG.Util.Image");
        Image::buildImageVerify(4,1,$type);
    }

	public function smail($to,$name,$subject = '',$other= '',$type){
		if(!empty($type)){
			$Tpl=M('Email_tpl');
			$map['type']=$type;
			$tpl=$Tpl->where($map)->find();
			switch($type){
				case 'reg':
					$body=str_replace('{member_name}',$name,$tpl['tpl']);
					break;
				case 'order':
					$body=str_replace('{member_name}',$name,$tpl['tpl']);
				    $body=str_replace('{order_id}',$other,$body);
					break;
				case 'pay':;break;
				case 'ship':;break;
			}
			$this->think_send_mail($to,$name,$subject,$body,null);
		}
	}

	/**
	 * 系统邮件发送函数
	 * @param string $to    接收邮件者邮箱
	 * @param string $name  接收邮件者名称
	 * @param string $subject 邮件主题 
	 * @param string $body    邮件内容
	 * @param string $attachment 附件列表
	 * @return boolean 
	 */
	public function think_send_mail($to, $name, $subject = '', $body = '', $attachment = null){
		$config = C('THINK_EMAIL');
		vendor('PHPMailer.class#phpmailer'); 
		$mail             = new PHPMailer(); 
		$mail->CharSet    = 'UTF-8';
		$mail->IsSMTP();  // 设定使用SMTP服务
		$mail->SMTPDebug  = 0;
		$mail->SMTPAuth   = true;
		//$mail->SMTPSecure = 'ssl';// 使用安全协议
		$mail->Host       = $config['SMTP_HOST'];
		$mail->Port       = $config['SMTP_PORT'];
		$mail->Username   = $config['SMTP_USER'];
		$mail->Password   = $config['SMTP_PASS'];
		$mail->SetFrom($config['FROM_EMAIL'], $config['FROM_NAME']);
		$replyEmail       = $config['REPLY_EMAIL']?$config['REPLY_EMAIL']:$config['FROM_EMAIL'];
		$replyName        = $config['REPLY_NAME']?$config['REPLY_NAME']:$config['FROM_NAME'];
		$mail->AddReplyTo($replyEmail, $replyName);
		$mail->Subject    = $subject;
		$mail->MsgHTML($body);
		$mail->AddAddress($to, $name);
		if(is_array($attachment)){ // 添加附件
			foreach ($attachment as $file){
				is_file($file) && $mail->AddAttachment($file);
			}
		}
		return $mail->Send() ? true : $mail->ErrorInfo;
	}
	//邮件end

	public function send_sms($user,$order_id){
		$Order=M('Orders');
		$map['order_id']=$order_id;
		$order=$Order->where($map)->find();
		$msg="亲爱的用户:".$user."您的订单:".$order_id."付款成功！商城将很快为您送货！亲耐心等候~";
		$name=C('SMS_NAME');
		$pwd=C('SMS_PWD');
		$dst=$order['ship_mobile'];
		$re=file_get_contents("http://203.81.21.34/send/gsend.asp?name={$name}&pwd={$pwd}&dst={$dst}&msg={$msg}&time={$time}");
	}

	//error
	public function error(){
		$this->cat=R('Goods/goods_cat');
		$this->display();
	}

    //商品浏览cookie
	public function goods_view($id){
		$goods_view=unserialize(stripslashes(cookie('goods_view')));
		if(empty($goods_view)){
			$goods_view=array();
		}
		
		$pp=0;
		for($j=0;$j<count($goods_view);$j++){
			if($goods_view[$j]==$id){
				$pp=$j;
				break;
			}
		}
		if($pp==0){
			array_push($goods_view,$id);
            cookie('goods_view',serialize($goods_view),C('CART_COOKIE'));  //设置cookie
			$Goods=M('Goods');
			$Goods->where('goods_id='.$id)->setInc('view_count');
		}

		
	}

	public function clear_cookie(){
		cookie('goods_view',null);
	}

	//找回密码
	public function get_password(){
		$action=$this->_get('action');
		$email=$_SESSION['getemail'];
		if(!empty($email)){
			$pos=strpos($email,'@');
			$this->elink="http://email.".substr($email,$pos+1,40);
		}

		if($action=='second'&&empty($email)){
			$this->redirect('Public/get_password');
		}

		if($action=='third'){
			$Pwd=M('Get_password');
			$map['token']=$this->_get('token');
			$map['status']='active';
			$ck=$Pwd->where($map)->find();
			if($ck){
				$this->token=$map['token'];
				$this->isvolid='1';
			}else{
				$this->isvolid='0';
			}
		}
		$this->assign('action',$action);
		$this->display();
	}

	public function yzuser(){
		$Member=M('Member');
		$Pwd=M('Get_password');
		$yzm=$this->_post('yzm');
		$map['email']=$this->_post('email');
		if($_SESSION['verify'] != md5($yzm))
		{
			echo 2;
		}
		else
		{
			$member=$Member->where($map)->find();
			if(false==$member)
			{
				echo 1;
			}
			else
			{
				$ip=get_client_ip();
				$data['pro_ip']=ip2long($ip);
				$data['email']=$map['email'];
				$data['member_id']=$member['member_id'];
				$data['token']=sha1($data['member_id']).time().md5($data['email']);
				$data['pro_time']=time();
				$data['status']='active';
				$host=C('WEB_HOST');
				$link="http://".$host."/Public/get_password/?action=third&token=".$data['token'];
				$body="{$member['uname']},您在蚂蚁街选择了密码找回，请点击以下链接完成操作：<br><a href='{$link}'>{$link}</a><br>如果您未进行此操作，请忽略此邮件！";
				$this->think_send_mail($map['email'],$member['uname'],$subject = '密码找回-蚂蚁街',$body,$attachment = null);
				if($Pwd->add($data)){$_SESSION['getemail']=$data['email'];echo $data['email'];}else{echo 2;}
			}
		}
	}

	public function setpwd(){
		$Member=M('Member');
		$Pwd=M('Get_password');
		$map['token']=$this->_post('token');
		$pwd=$this->_post('pwd');
		$map['status']='active';
		$ck=$Pwd->where($map)->find();
		if($ck){
			$data['status']='dead';
			$Pwd->where($map)->save($data);
			unset($_SESSION['getemail']);
			$map2['member_id']=$ck['member_id'];
			$data2['password']=md5($pwd);
			if($Member->where($map2)->save($data2)){echo 1;}else{echo 0;}
		}else{
			echo 0;
		}
	}
	//找回密码结束



}
?>